package br.ufpe.cin.rdfilter.util;

import java.util.ArrayList;
import java.util.Iterator;

import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.sparql.core.TriplePath;
import com.hp.hpl.jena.sparql.syntax.ElementPathBlock;
import com.hp.hpl.jena.sparql.syntax.ElementVisitorBase;
/**
 * 
 * @author Alberto
 * This class get all predicates in the Graph Patterns on SPARQL queries.
 * Each predicate is stored and ordered in a list. 
 *
 */
public class ElementVisitorPredicates extends ElementVisitorBase{

	private ArrayList<String> visitedPredicates;
	
	public ElementVisitorPredicates(){
		this.visitedPredicates = new ArrayList<String>();
	}
	
	public ArrayList<String> getVisitedPredicates() {
		return visitedPredicates;
	}
	
	public void visit(ElementPathBlock el) {
		
		Node predicate;
		
		Iterator<TriplePath> iterator = el.getPattern().iterator();
		while (iterator.hasNext()) {
			TriplePath triplePath = iterator.next();
			
			predicate = triplePath.getPredicate();
			
			// only if the predicate is a URI, it will inserted into the list
			if(predicate.isURI()){
				visitedPredicates.add(predicate.toString());
			}
			
		}
		super.visit(el);
	}
	
	
	
}
